/*
 * @Description:日期组件点击
 * @version: V1.0.0
 * @Author: Shuangshuang Song
 * @Date: 2020-08-20 14:10:26
 * @LastEditTime: 2020-10-27 17:10:56
 * @LastEditors: Shuangshuang Song
 */
const clickoutsideContext = '@@clickoutsideContext';

export default {
  name: 'click-outside',
  bind(el, binding, vnode) {
    const documentHandler = function (e) {
      // 解决日期，月份选择 点击年或者月 选择   popover 关闭的问题
      if (!vnode.context || el.contains(e.target) || e.target.className === 'mu-year-button'
      || e.target.className === 'mu-year-button-text' || e.target.className === 'mu-month-button-bg'
      || e.target.className === 'mu-month-button-text' || e.target.className === 'mu-month-button'
      || e.target.className === 'mu-datepicker-toolbar-title clickable') return;

      if (binding.expression) {
        vnode.context[el[clickoutsideContext].methodName](e);
      } else {
        el[clickoutsideContext].bindingFn(e);
      }
    };
    el[clickoutsideContext] = {
      documentHandler,
      methodName: binding.expression,
      bindingFn: binding.value,
    };
    setTimeout(() => {
      document.addEventListener('click', documentHandler);
    }, 0);
  },

  update(el, binding) {
    el[clickoutsideContext].methodName = binding.expression;
    el[clickoutsideContext].bindingFn = binding.value;
  },

  unbind(el) {
    document.removeEventListener('click', el[clickoutsideContext].documentHandler);
  },
};
